<template>
  <div class="app-container server-monitor">
    
    <LastUpdate :time="time" :onClickUpdate="handleClickUpdate"/>

    <el-card >
      <el-table v-loading="tableLoading" :data="dataList" stripe border >
        <el-table-column label="参数" align="left" prop="param" sortable >
          <template slot-scope="{row}">
            <el-tag :color="textInfo[row.param].color">{{ row.param }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="描述" align="left" prop="text"  >
          <template slot-scope="{row}">
            {{ textInfo[row.param].text }}
          </template>
        </el-table-column>
        <el-table-column label="当前值" align="left" :show-overflow-tooltip="true" >
          <template slot-scope="{row}">
            {{ row.value }} {{ textInfo[row.param].unit }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
  import {httpGet} from '@/api';

  export default {
    data() {
      return {
        time: '',
        tableLoading: true,
        dataList: [],
        // 列表通过 textInfo 渲染出颜色、描述和单位
        textInfo: {
          'system.cpu.count': { color: '#e8f4ff', text: 'CPU 数量', unit: '核' },
          'system.cpu.usage': { color: '#e8f4ff', text: '系统 CPU 使用率', unit: '%' },
          'process.start.time': { color: '#f5cfd5', text: '应用启动时间点', unit: '' },
          'process.uptime': { color: '#f5cfd5', text: '应用已运行时间', unit: '秒' },
          'process.cpu.usage': { color: '#f5cfd5', text: '当前应用 CPU 使用率', unit: '%' }
        },
        // 当一条记录中需要取出多条数据的时候需要配置该字段
        moreInfo: {}
      }
    },
    mounted() {
      this.loadTomcatInfo()
    },
    methods: {

      handleClickUpdate() {
        this.loadTomcatInfo()
      },

      loadTomcatInfo() {
        this.tableLoading = true
        this.time = this.$formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss')
        Promise.all([
          httpGet('actuator/metrics/system.cpu.count'),
          httpGet('actuator/metrics/system.cpu.usage'),
          httpGet('actuator/metrics/process.start.time'),
          httpGet('actuator/metrics/process.uptime'),
          httpGet('actuator/metrics/process.cpu.usage')
        ]).then((res) => {
          let info = []
          res.forEach((value, id) => {
            let more = this.moreInfo[value.name]
            if (!(more instanceof Array)) {
              more = ['']
            }
            more.forEach((item, idx) => {
              let param = value.name + item
              let val = value.measurements[idx].value
              if (param === 'system.cpu.usage' || param === 'process.cpu.usage') {
                val = this.convert(val, Number)
              }
              if (param === 'process.start.time') {
                val = this.convert(val, Date)
              }
              info.push({ id: param + id, param, text: 'false value', value: val })
            })
          })
          this.dataList = info
        }).catch((e) => {
          console.error(e)
          this.$message.error('获取服务器信息失败')
        }).finally(() => {
          this.tableLoading = false
        })
      },

      convert(value, type) {
        if (type === Number) {
          return Number(value * 100).toFixed(2)
        } else if (type === Date) {
          return this.$formatDate(value * 1000, 'yyyy-MM-dd hh:mm:ss')
        }
        return value
      }
    }
  }
</script>
<style></style>
